<?php


namespace App\Http\Services;

use App\Http\Model\FullScreen;
use App\Http\Model\Hint;
use Illuminate\Support\Facades\Redis;

class FullScreenServices extends CommonServices
{

    protected $model;

    public function __construct()
    {
        $this->model = new FullScreen();
    }

    public function add($params)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        $error = '';
        try {
            $data = $this->checkParams($params);
            if (empty($data)){
                $code = Hint::PARAM_ERROR;
                $msg = '参数不正确';
                throw new \Exception($msg);
            }

            $res = $this->model -> add($data);

            if (!$res){
                $code = Hint::PARAM_ERROR;
                $msg = '插入失败';
                throw new \Exception($msg);
            }
        } catch (\Exception $e) {

            $error = $e->getMessage() . '-line:' . $e->getLine();
            $code = ($code != Hint::SUCCESS) ? $code : Hint::ERR_FAIL;
            $msg .= ' 操作失败';
        }

        write_log(
            'FullScreenServices'.(isCli()?'-cli':''),
            ($code == Hint::SUCCESS ? '[INFO]':'[ERROR]').' Add',
            ['error'=>$error, 'args'=>func_get_args()]
        );

        return compact('code', 'msg', 'data');
    }

    public function edit($params)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        $error = '';

        $id = $params['id'] ?? 0;

        try {
            if ($id<=0){
                $code = Hint::PARAM_ERROR;
                $msg = '飘萍ID必需';
                throw new \Exception($msg);
            }
            $info = $this->model -> find($id);
            if (empty($info)) {
                $code = Hint::PARAM_ERROR;
                $msg = '无效飘萍';
                throw new \Exception($msg);
            }
            $data = $this->checkParams($params);

            $res = $this->model -> editById($id, $data);

            if (!$res){
                $code = Hint::PARAM_ERROR;
                $msg = '飘萍编辑失败';
                throw new \Exception($msg);
            }
        } catch (\Exception $e) {

            $error = $e->getMessage() . '-line:' . $e->getLine();
            $code = ($code != Hint::SUCCESS) ? $code : Hint::ERR_FAIL;
            $msg .= ' 操作失败';
        }

        write_log(
            'FullScreenServices'.(isCli()?'-cli':''),
            ($code == Hint::SUCCESS ? '[INFO]':'[ERROR]').' Edit',
            ['error'=>$error, 'args'=>func_get_args()]
        );

        return compact('code', 'msg', 'data');
    }

    public function del($params)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        $error = '';

        $id = $params['id'] ?? 0;

        try {
            if ($id<=0){
                $code = Hint::PARAM_ERROR;
                $msg = '飘萍ID必需';
                throw new \Exception($msg);
            }
            $info = $this->model -> find($id);

            if (empty($info)) {
                $code = Hint::PARAM_ERROR;
                $msg = '无效飘萍';
                throw new \Exception($msg);
            }

            $res = $this->model -> del($id);

            if (!$res){
                $code = Hint::PARAM_ERROR;
                $msg = '飘萍删除失败';
                throw new \Exception($msg);
            }
        } catch (\Exception $e) {

            $error = $e->getMessage() . '-line:' . $e->getLine();
            $code = ($code != Hint::SUCCESS) ? $code : Hint::ERR_FAIL;
            $msg .= ' 操作失败';
        }

        write_log(
            'FullScreenServices'.(isCli()?'-cli':''),
            ($code == Hint::SUCCESS ? '[INFO]':'[ERROR]').' Del',
            ['error'=>$error, 'args'=>func_get_args()]
        );

        return compact('code', 'msg', 'data');
    }


    public function fullList()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = $this->model -> fullList();

        return compact('code', 'msg', 'data');
    }

    public function getFullScreen($id)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        if ($id){
            $data = $this->model -> getFullScreen($id);
        }

        return compact('code', 'msg', 'data');
    }

    public function getFullPage()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = $this->model ->getFullPage();
        return compact('code', 'msg', 'data');
    }

    public function checkParams($params)
    {
        $data = [];
        $data['name'] = '';
        $data['bg_img_url'] = '';
        $data['action_img_url'] = '';
        $data['action'] = '';
        $data['text'] = '';
        $data['screen_config'] = '';
        $data['room_ids'] = 0;

        if (isset($params['name'])){
            $data['name'] =  (string) trim( $params['name'] );
        }

        if (isset($params['bg_img_url'])){
            $data['bg_img_url'] =  (string) trim( $params['bg_img_url'] );
        }

        if (isset($params['action_img_url'])){
            $data['action_img_url'] =  (string) trim( $params['action_img_url'] );
        }

        if (isset($params['action'])){
            $data['action'] =  (string) trim( $params['action'] );
        }

        if (isset($params['text'])){
            $data['text'] =  (string) trim( $params['text'] );
        }

        if (isset($params['screen_config'])){
            $data['screen_config'] =  (string) trim( $params['screen_config'] );
        }

        $pages = '';
        if (isset($params['show_pages'])){
            $show_pages =  (string) trim( $params['show_pages'] );
            $fullPage = $this->model -> getFullPage();
            $fullPageId = array_keys( $fullPage );
            $pages = explode(',', $show_pages);
            $pages = array_filter($pages, function ($val)use($fullPageId){
                return in_array($val, $fullPageId);
            });
            $pages = implode(',',$pages);
        }
        $data['show_pages'] = $pages;
        if(isset($params['room_ids'])){
            $data['room_ids'] = $params['room_ids'];
        }
        return $data;
    }

}
